package swc.smu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.List;

import swc.smu.bean.ExpandListVO;
import swc.smu.ui.R;

/**
 * Created by smu on 2016/1/27.
 */
public class ExpandAdapter extends BaseExpandableListAdapter {

    Context context;
    List<ExpandListVO> dataList;
    LayoutInflater mInflater;

    public ExpandAdapter(Context context, List<ExpandListVO> dataList) {
        this.context = context;
        this.dataList = dataList;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return dataList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        ExpandListVO expandListVO = getGroup(groupPosition);
        List<String> list = expandListVO.getDataList();
        return list.size();
    }

    @Override
    public ExpandListVO getGroup(int groupPosition) {
        return dataList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return getGroup(groupPosition).getDataList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder pViewHolder;
        if (convertView == null) {
            pViewHolder = new ParentViewHolder();
            convertView = mInflater.inflate(R.layout.item_expand_list_parent, null);
            pViewHolder.pTextView = (TextView) convertView.findViewById(R.id.expand_list_parent_text);
        } else {
            pViewHolder = (ParentViewHolder) convertView.getTag();
        }
        pViewHolder.pTextView.setText(dataList.get(groupPosition).getTitle());
        convertView.setTag(pViewHolder);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if(convertView == null) {
            childViewHolder = new ChildViewHolder();
            convertView = mInflater.inflate(R.layout.item_expand_list_child, parent, false);
            childViewHolder.cTextView = (TextView) convertView.findViewById(R.id.expand_list_child_text);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        childViewHolder.cTextView.setText(dataList.get(groupPosition).getDataList().get(childPosition));
        convertView.setTag(childViewHolder);
        return convertView;
    }

    class ParentViewHolder {
        TextView pTextView;
    }

    class ChildViewHolder {
        TextView cTextView;
    }

    /**
     * 判断指定位置的子项是否选中 默认选中
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
